package com.nulldev.util.web.HttpServer3.extensions.http2.api_compat;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import com.nulldev.util.data.Variables;
import com.nulldev.util.io.IOUtils;
import com.nulldev.util.web.HttpServer3.extensions.http2.compat.EntityEncodings;
import com.nulldev.util.web.HttpServer3.extensions.http2.compat.HttpBody;

public class HttpBodyOutputStream implements HttpBody {

	private final InputStream is;

	public HttpBodyOutputStream(final InputStream is) {
		this.is = Variables.requireNonNullObject(is, "is");
	}

	@Override
	public void close() throws IOException {
		this.is.close();
	}

	@Override
	public EntityEncodings getAcceptedEncoding() {
		return EntityEncodings.IDENTITY;
	}

	@Override
	public void writeToBody(int b) throws IOException {
		// TODO Auto-generated method stub

	}

	@Override
	public void writeToBody(byte[] b) throws IOException {
		// TODO Auto-generated method stub

	}

	@Override
	public void writeToBody(byte[] b, int length) throws IOException {
		// TODO Auto-generated method stub

	}

	@Override
	public void maxCapacity(int maxCap) {
		// TODO Auto-generated method stub

	}

	@Override
	public byte[] getBytes() {
		try {
			return IOUtils.toByteArray(is);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public byte[] getBytes(int length) {
		try {
			return IOUtils.readFully(is, 0, length);
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}

	@Override
	public int available() throws IOException {
		return this.is.available();
	}

	@Override
	public String string() {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public void streamBytesTo(OutputStream os) throws IOException {
		IOUtils.copy(is, os);
	}

	@Override
	public int length() {
		try {
			return this.is.available();
		} catch (IOException e) {
			throw new RuntimeException(e);
		}
	}


}
